package com.mall.order.services;

import com.mall.order.IOrederDetailService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.converter.OrderConverter;
import com.mall.order.dal.entitys.Order;
import com.mall.order.dal.entitys.OrderItem;
import com.mall.order.dal.entitys.OrderShipping;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dal.persistence.OrderShippingMapper;
import com.mall.order.dto.*;
import com.mall.user.IMemberService;
import com.mall.user.dal.entitys.Member;
import com.mall.user.dto.QueryMemberRequest;
import com.mall.user.dto.QueryMemberResponse;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.List;


/**
 * \* Description:
 * \
 */
@Service
public class IOrederDetailServiceImpl implements IOrederDetailService {


    @Reference
    IMemberService iMemberService;

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderShippingMapper orderShippingMapper;

    @Autowired
    OrderConverter orderConverter;

    @Override
    public NewOrderDetailResponse showOrderDetail(NewOrderDetailRequest request) {
        NewOrderDetailResponse response = new NewOrderDetailResponse();
        QueryMemberRequest queryMemberRequest = new QueryMemberRequest();
        try {
            request.requestCheck();
            //获取用户id和用户name
            Long userId = request.getUserId();
            String orderId = request.getOrderId();
            queryMemberRequest.setUserId(userId);
            QueryMemberResponse queryMemberResponse = iMemberService.queryMemberById(queryMemberRequest);
            String username = queryMemberResponse.getUsername();
            NewOrderDetailDto newOrderDetailDto = new NewOrderDetailDto();
            newOrderDetailDto.setUserName(username);
            newOrderDetailDto.setUserId(userId);


            Example example1 = new Example(OrderItem.class);
            example1.createCriteria().andEqualTo("orderId", orderId);
            List<OrderItem> orderItems = orderItemMapper.selectByExample(example1);
            List<NewOrderItemDto> orderItemDtos = orderConverter.orderItemToDtos(orderItems);
            BigDecimal total = new BigDecimal(0);
            for (NewOrderItemDto orderItemDto : orderItemDtos) {
                BigDecimal price = orderItemDto.getPrice();
                Integer num = orderItemDto.getNum();
                BigDecimal temp = new BigDecimal(num);
                BigDecimal priceTemp = price.multiply(temp);
                total = total.add(priceTemp);
            }


            newOrderDetailDto.setOrderTotal(total);
            Order order = orderMapper.selectByPrimaryKey(orderId);
            Integer status = order.getStatus();
            newOrderDetailDto.setOrderStatus(status);
            newOrderDetailDto.setGoodsList(orderItemDtos);
            OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(orderId);
            String receiverAddress = orderShipping.getReceiverAddress();
            String receiverMobile = orderShipping.getReceiverMobile();
            newOrderDetailDto.setStreetName(receiverAddress);
            newOrderDetailDto.setTel(receiverMobile);
            response.setNewOrderDetailDto(newOrderDetailDto);
            response.setCode(OrderRetCode.SUCCESS.getCode());
            response.setMsg(OrderRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();

            response.setCode(OrderRetCode.SYSTEM_ERROR.getCode());
            response.setMsg(OrderRetCode.SYSTEM_ERROR.getMessage());
        }
        return response;
    }

    @Override
    public DeleteOrderResponse deleteOrder(DeleteOrderRequest request) {
        DeleteOrderResponse response = new DeleteOrderResponse();
      try {
          request.requestCheck();
          String orderId = request.getOrderId();
          Order order = orderMapper.selectByPrimaryKey(orderId);
          Integer status = order.getStatus();
          if (status > 3||status== 0 ){
              orderMapper.deleteByPrimaryKey(orderId);
              Example example = new Example(OrderItem.class);
              example.createCriteria().andEqualTo("orderId", orderId);
              orderItemMapper.deleteByExample(example);
              orderShippingMapper.deleteByPrimaryKey(orderId);
              response.setCode(OrderRetCode.SUCCESS.getCode());
              response.setMsg(OrderRetCode.SUCCESS.getMessage());
          }else {
              response.setCode(OrderRetCode.FAIEL.getCode());
              response.setMsg(OrderRetCode.FAIEL.getMessage());
          }
      }catch (Exception e) {
          e.printStackTrace();
          response.setCode(OrderRetCode.SYSTEM_ERROR.getCode());
          response.setMsg(OrderRetCode.SYSTEM_ERROR.getMessage());
      }
        return response;
    }
}
